<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/1/10
 * Time: 15:09
 */

namespace app\admin\service;


use app\admin\model\Log;
use Page\Page;

class LogService
{
    private $model;
    public function __construct()
    {
        $this->model = new Log();
    }
    public function getAllLogPage($post)
    {
        $page = new Page($this->model,$post['page'],$post['limit'],[],['add_time desc']);
        $log = $page->layuiPage();
        return $log;
    }

    public function byConditionGetLogData($post)
    {
        $arr = $this->SpliceWhereArr($post);
        $page = new Page($this->model,$arr['page'],$arr['limit'],[],[],$arr['where']);
        $result = $page->layuiPage();
        return $result;
    }

    private function SpliceWhereArr($post)
    {
        $page = $post['page'];
        $limit = $post['limit'];

        $filed = $post['field'];
        $time = $filed['time'];
        if(!strstr($time,'.')){
            $time_start = '';
            $time_end = '';
        }else {
            $date = explode('.', $filed['time']);
            $time_start = $date[0];
            $time_end = $date[1];
        }

        $arr = [
            'username' => ['username','=',$filed['username']],
            'time' => ['add_time', 'between time', [$time_start, $time_end]]
        ];

        # 组装where查询条件数组
        $where = [];
        foreach ($filed as $key => $value){
            if($value == ''){
                unset($arr[$key]);
                continue;
            }
            array_push($where,$arr[$key]);
        }
        return [
            'page' => $page,
            'limit' => $limit,
            'where' => $where
        ];
    }
}